import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import struct
import os

class FileSizeToHexConverter:
    def __init__(self, root):
        self.root = root
        self.root.title("十六进制转换工具")
        self.root.geometry("500x320")
        self.root.resizable(False, False)
        
        # 设置主题样式
        self.style = ttk.Style()
        self.style.theme_use('clam')
        
        self.create_widgets()
        
    def create_widgets(self):
        # 主框架
        main_frame = ttk.Frame(self.root, padding="10")
        main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
        
        # 文件选择部分
        file_frame = ttk.LabelFrame(main_frame, text="文件选择", padding="5")
        file_frame.grid(row=0, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
        
        self.file_path = tk.StringVar()
        ttk.Entry(file_frame, textvariable=self.file_path, width=50).grid(row=0, column=0, padx=5)
        ttk.Button(file_frame, text="浏览...", command=self.browse_file).grid(row=0, column=1, padx=5)
        
        # 转换选项部分
        options_frame = ttk.LabelFrame(main_frame, text="转换选项", padding="5")
        options_frame.grid(row=1, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
        
        self.byte_order = tk.StringVar(value="big")
        ttk.Radiobutton(options_frame, text="大端序 (Big Endian)", variable=self.byte_order, value="big").grid(row=0, column=0, sticky=tk.W)
        ttk.Radiobutton(options_frame, text="小端序 (Little Endian)", variable=self.byte_order, value="little").grid(row=0, column=1, sticky=tk.W)
        
        # 结果显示部分
        result_frame = ttk.LabelFrame(main_frame, text="转换结果", padding="5")
        result_frame.grid(row=2, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
        
        self.result_var = tk.StringVar()
        ttk.Entry(result_frame, textvariable=self.result_var, width=50, state="readonly").grid(row=0, column=0, padx=5)
        ttk.Button(result_frame, text="复制", command=self.copy_result).grid(row=0, column=1, padx=5)
        
        # 按钮部分
        button_frame = ttk.Frame(main_frame)
        button_frame.grid(row=3, column=0, columnspan=2, pady=10)
        
        ttk.Button(button_frame, text="转换", command=self.convert).pack(side=tk.LEFT, padx=5)
        ttk.Button(button_frame, text="清除", command=self.clear).pack(side=tk.LEFT, padx=5)
        ttk.Button(button_frame, text="退出", command=self.root.quit).pack(side=tk.LEFT, padx=5)  # 修正了这里的语法错误
        
        # 状态栏
        self.status_var = tk.StringVar(value="就绪")
        status_bar = ttk.Label(self.root, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W)
        status_bar.grid(row=1, column=0, sticky=(tk.W, tk.E))
        
    def browse_file(self):
        filename = filedialog.askopenfilename(title="选择文件")
        if filename:
            self.file_path.set(filename)
            self.status_var.set(f"已选择文件: {os.path.basename(filename)}")
    
    def convert(self):
        file_path = self.file_path.get()
        if not file_path or not os.path.exists(file_path):
            messagebox.showerror("错误", "请选择有效的文件")
            return
            
        try:
            # 获取文件大小
            file_size = os.path.getsize(file_path)
            
            # 根据选择的字节序进行转换
            if self.byte_order.get() == "big":
                packed_size = struct.pack('>L', file_size)  # 大端序
            else:
                packed_size = struct.pack('<L', file_size)  # 小端序
                
            # 转换为十六进制字符串
            hex_size = packed_size.hex().upper()
            
            # 显示结果
            self.result_var.set(hex_size)
            self.status_var.set(f"转换成功: {file_size} 字节 -> {hex_size}")
            
        except Exception as e:
            messagebox.showerror("错误", f"转换过程中发生错误: {str(e)}")
            self.status_var.set("转换失败")
    
    def copy_result(self):
        result = self.result_var.get()
        if result:
            self.root.clipboard_clear()
            self.root.clipboard_append(result)
            self.status_var.set("结果已复制到剪贴板")
        else:
            messagebox.showwarning("警告", "没有可复制的内容")
    
    def clear(self):
        self.file_path.set("")
        self.result_var.set("")
        self.status_var.set("已清除")

if __name__ == "__main__":
    root = tk.Tk()
    app = FileSizeToHexConverter(root)
    root.mainloop()